import { getList as _getList } from './api';
import { Dispatch } from 'redux';
import type { IStateProp } from './index.d'

export const MODULE_NAME: string = 'CampusLibrary/BookInventory';

export const types = {
  TABLE_DATA: `${MODULE_NAME}/TABLE_DATA`, // 列表数据
}

export default (state: IStateProp = {
  tableData: []
}, action: any) => {
  switch (action.type) {
    case types.TABLE_DATA:
      return Object.assign({}, state, {
        tableData: action.data
      })
    default: return state
  }
}

/**
 * 获取列表数据
*/
export const getList = () => {
  return async (dispatch: Dispatch): Promise<void> => {
    try {
      const res = await _getList();
      dispatch({
        type: types.TABLE_DATA,
        data: res.data
      })
    } catch {}
  }
}
